<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use app\admin\model\Duty;
use app\admin\model\Reply;
use app\admin\model\RobotAction;
use app\common\CommandCron;
use app\sys\model\Hint;
use GatewayWorker\Lib\Gateway;
use think\facade\Env;
use think\facade\Log;
use Workerman\Crontab\Crontab;
use Workerman\Lib\Timer;

// +----------------------------------------------------------------------
// | Workerman设置 仅对 php think worker:server 指令有效
// +----------------------------------------------------------------------
return [
    // 扩展自身需要的配置
    'protocol'       => 'websocket', // 协议 支持 tcp udp unix http websocket text
    'host'           => '0.0.0.0', // 监听地址
    'port'           => 2345, // 监听端口
    'socket'         => '', // 完整监听地址
    'context'        => [], // socket 上下文选项
    'worker_class'   => '', // 自定义Workerman服务类名 支持数组定义多个服务

    // 支持workerman的所有配置参数
    'name'           => 'thinkphp',
    'count'          => 1,
    'daemonize'      => false,
    'pidFile'        => Env::get('runtime_path') . 'worker.pid',

    // 支持事件回调 //
    // onWorkerStart
    'onWorkerStart'  => function ($worker) {

        new Crontab('50 1 * * *', function(){
            // 是否离职
            CommandCron::userList();

            // 修改状态
            CommandCron::autoUpStaffStatus();

            // redis 删除
            $dutyIdArr = Duty::getServerData("af");
            \app\admin\model\Staff::setStaffToList($dutyIdArr,"af");
            $dat = date("Y-m-d H:i:s");
            Log::info("定时器:{$dat}");
        });

        // 1秒定时查询
       Timer::add(1, function(){
            // 自动回复
            //\app\common\service\ReplyService::autoReply();//新自动回复功能
            Reply::autoReplayInfo();
            $dat = date("Y-m-d H:i:s");
            Log::info("定时器1秒一次{$dat}");

            // 行为反馈
           RobotAction::actionToUser();
        });


        // 60秒定时查询 （//超时回复）
        Timer::add(60, function(){
            // 自动回复
            Hint::chatTimeEnd2();

            // 客服在线时间
            \app\admin\model\StaffTime::saveTimeNow();
            $dat = date("Y-m-d H:i:s");
            Log::info("定时器60秒一次{$dat}");
        });

    },
    // onWorkerReload
    'onWorkerReload' => function ($worker) {

    },
    // onConnect
    'onConnect'      => function ($connection) {
        $client_id = $connection->id;
        $connection->send(json_encode(array(
            'type' => 'init',
            'client_id' => $client_id
        )));
    },
    // onMessage
    'onMessage'      => function ($connection, $data) {
        $connection->send('receive success');
    },
    // onClose
    'onClose'        => function ($connection) {

    },
    // onError
    'onError'        => function ($connection, $code, $msg) {
        echo "error [ $code ] $msg\n";
    },
];
